use ordered_float::NotNan;

type Map = std::collections::BTreeMap<NotNan<f64>, f64>;

pub struct PriceQty {
    price: NotNan<f64>,
    qty: f64,
}

pub struct BookBuilder {
    pub timestamp: Option<i64>,
    pub bids: Map,
    pub asks: Map,
}

impl BookBuilder {
    pub fn new() -> BookBuilder {
        BookBuilder {
            timestamp: None,
            bids: Map::new(),
            asks: Map::new(),
        }
    }
    pub fn on_snapshot(
        &mut self,
        timestamp: i64,
        asks: &Vec<(NotNan<f64>, f64)>,
        bids: &Vec<(NotNan<f64>, f64)>,
    ) {
        self.clear();
        self.on_diff(timestamp, asks, bids);
    }
    pub fn on_diff(
        &mut self,
        timestamp: i64,
        asks: &Vec<(NotNan<f64>, f64)>,
        bids: &Vec<(NotNan<f64>, f64)>,
    ) {
        self.timestamp = Some(timestamp);
        for ask in asks {
            self.asks.insert(ask.0, ask.1);
        }
        for bid in bids {
            self.bids.insert(bid.0, bid.1);
        }
        // if let Some(pq) = asks.min() {
        //     self.bids.split_off(&pq.price);
        // }
        // if let Some(pq) = bids.max() {
        //     self.asks = self.asks.split_off(&(pq.price+1e-8));
        // }
    }
    pub fn clear(&mut self) {
        self.asks.clear();
        self.bids.clear();
    }
    fn ask0(&self) -> Option<PriceQty> {
        self.asks
            .iter()
            .next()
            .map(|(k, v)| PriceQty { price: *k, qty: *v })
    }
    fn bid0(&self) -> Option<PriceQty> {
        self.bids
            .iter()
            .next_back()
            .map(|(k, v)| PriceQty { price: *k, qty: *v })
    }
}
